<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>
    
    枚举类型 - DSRBLOG
    
  </title>
  

  
  
  <link href="atom.xml" rel="alternate" title="DSRBLOG" type="application/atom+xml">
  <link rel="stylesheet" href="asset/css/style.min.css">
  <link rel="stylesheet" href="asset/css/doc.css">
  <script src="asset/app.js"></script>


</head>

<body> <section class="ct-body">
	<div class="container">
		<div class="columns is-variable bd-klmn-columns is-4 is-centered">
			<div class="column is-four-fifths">
				<div class="post-body single-content">
					
					<h1 class="title">
						枚举类型
					</h1>


					<div class="media">
						
						
						<div class="media-content">
							<div class="content">
								<p>
									<span class="date">2022/01/28 01:52 上午</span>
									

									

									<br />
									<span class="tran-tags">Tags:</span>&nbsp;
									
									<a class="tag is-link is-light">#C语言</a>
									

								</p>
							</div>
						</div>

					</div>
				</div>
				<article class="markdown-body single-content">
					<h2><a id="%E6%9E%9A%E4%B8%BE%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%AE%9A%E4%B9%89" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>枚举类型的定义</h2>
<ul>
<li>如果一个变量只有几种可能的值，则可以定义为枚举类型</li>
<li>所谓“枚举”是指将变量的值一一列举出来，变量的值限定在列举出来的值的范围内</li>
</ul>
<h3><a id="%E5%A3%B0%E6%98%8E%E6%9E%9A%E4%B8%BE%E7%B1%BB%E5%9E%8B%E7%9A%84%E6%96%B9%E6%B3%95" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>声明枚举类型的方法</h3>
<pre class="line-numbers"><code class="language-c">enum 枚举名 {枚举元素1, 枚举元素2,...}; //枚举元素（枚举常量）只能以标识符形式表示，
</code></pre>
<h4><a id="%E4%B8%BE%E4%BE%8B" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>举例</h4>
<pre class="line-numbers"><code class="language-c">enum weekday {
    sun, mon = 2, tur, wed, thu, fri, sat
};
</code></pre>
<ul>
<li>第一个枚举元素的默认值为0，后一个枚举成员的值为前一个成员的值+1</li>
<li>可以在<strong>定义枚举类型时</strong>改变枚举元素的值，此处将第二个枚举元素指定为2，第一个元素为0，第三个元素为3，以此类推</li>
</ul>
<h2><a id="%E6%9E%9A%E4%B8%BE%E7%B1%BB%E5%9E%8B%E5%8F%98%E9%87%8F%E7%9A%84%E8%AF%B4%E6%98%8E" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>枚举类型变量的说明</h2>
<h3><a id="%E5%AE%9A%E4%B9%89%E6%9E%9A%E4%B8%BE%E5%8F%98%E9%87%8F" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>定义枚举变量</h3>
<h4><a id="1%E3%80%81%E5%85%88%E5%AE%9A%E4%B9%89%E6%9E%9A%E4%B8%BE%E7%B1%BB%E5%9E%8B%EF%BC%8C%E5%86%8D%E5%AE%9A%E4%B9%89%E6%9E%9A%E4%B8%BE%E5%8F%98%E9%87%8F" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>1、先定义枚举类型，再定义枚举变量</h4>
<pre class="line-numbers"><code class="language-c">enum weekday {
    sun, mon, tur, wed, thu, fri, sat
};

enum {
    sun, mon, tur, wed, thu, fri, sat
} weekday workday;
</code></pre>
<h4><a id="2%E3%80%81%E5%AE%9A%E4%B9%89%E6%9E%9A%E4%B8%BE%E7%B1%BB%E5%9E%8B%E7%9A%84%E5%90%8C%E6%97%B6%E5%AE%9A%E4%B9%89%E6%9E%9A%E4%B8%BE%E5%8F%98%E9%87%8F" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>2、定义枚举类型的同时定义枚举变量</h4>
<pre class="line-numbers"><code class="language-c">enum weekday {
    sun, mon, tur, wed, thu, fri, sat
}  workday;
</code></pre>
<h4><a id="3%E3%80%81%E7%9C%81%E7%95%A5%E6%9E%9A%E4%B8%BE%E5%90%8D%EF%BC%8C%E7%9B%B4%E6%8E%A5%E5%AE%9A%E4%B9%89%E6%9E%9A%E4%B8%BE%E5%8F%98%E9%87%8F" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>3、省略枚举名，直接定义枚举变量</h4>
<pre class="line-numbers"><code class="language-c">enum {
    sun, mon, tur, wed, thu, fri, sat
}  workday;
</code></pre>
<h2><a id="%E6%9E%9A%E4%B8%BE%E7%B1%BB%E5%9E%8B%E5%8F%98%E9%87%8F%E7%9A%84%E8%B5%8B%E5%80%BC%E5%92%8C%E4%BD%BF%E7%94%A8" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>枚举类型变量的赋值和使用</h2>
<ul>
<li><code>枚举元素</code>是常量，不是变量，不能在程序中用赋值语句再对它进行赋值</li>
</ul>
<pre class="line-numbers"><code class="language-c">sun = 2; //错误方法
</code></pre>
<ul>
<li>只能把<code>枚举元素</code>赋予枚举变量，不能把<code>枚举元素的数值</code>直接赋予枚举变量</li>
</ul>
<pre class="line-numbers"><code class="language-c">workday = sat; //正确
workday = 6; //错误
</code></pre>

				</article>
				<div class="comments-wrap">
					<div class="share-comments">
						

						

						
					</div>
				</div><!-- end comments wrap -->
			</div>
		</div><!-- end columns -->
	</div><!-- end container -->
</section>  














<script src="asset/prism.js"></script>









</body>

</html>